package com.sgb.kjwl.view.ui.activity.enterprise;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityMakeOverPublishUpBinding;
import com.sgb.kjwl.model.entity.enterprise.PublishCompanyDetailEntity;
import com.sgb.kjwl.model.entity.enterprise.PublishCopyrightDetailEntity;
import com.sgb.kjwl.model.entity.enterprise.PublishPatentDetailEntity;
import com.sgb.kjwl.model.entity.enterprise.PublishPropertyDetailEntity;
import com.sgb.kjwl.model.entity.enterprise.PublishTradeDetailEntity;
import com.sgb.kjwl.view.ui.fragment.enterprise.CompanyPublishFragment;
import com.sgb.kjwl.view.ui.fragment.enterprise.CopyrightPublishFragment;
import com.sgb.kjwl.view.ui.fragment.enterprise.PatentPublishFragment;
import com.sgb.kjwl.view.ui.fragment.enterprise.PropertyPublishFragment;
import com.sgb.kjwl.view.ui.fragment.enterprise.TradePublishFragment;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

public class MakeOverPublishActivity extends AppActivity {

    ActivityMakeOverPublishUpBinding mBinding;
    public MutableLiveData<Boolean> mLiveData = new MutableLiveData<>();
    public MutableLiveData<Intent> mIntentLiveData = new MutableLiveData<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_make_over_publish_up);

        mBinding.tvUpload.setOnClickListener(this);
        mBinding.actionBar.setOnBackClickListener(view -> finish());

        int type = getIntent().getIntExtra("type", 0);
        Serializable serializable = getIntent().getSerializableExtra("entity");
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        switch (type) {
            case 1:
                mBinding.actionBar.setTitle("商标转让");
                if (serializable != null){
                    fragmentTransaction.replace(R.id.rl_makeover_publish_up_content, TradePublishFragment.newInstance((PublishTradeDetailEntity) serializable));
                }else {
                    fragmentTransaction.replace(R.id.rl_makeover_publish_up_content, TradePublishFragment.newInstance());
                }
                break;
            case 2:
                mBinding.actionBar.setTitle("专利转让");
                if (serializable != null){
                    fragmentTransaction.replace(R.id.rl_makeover_publish_up_content, PatentPublishFragment.newInstance((PublishPatentDetailEntity) serializable));
                }else {
                    fragmentTransaction.replace(R.id.rl_makeover_publish_up_content, PatentPublishFragment.newInstance());
                }
                break;
            case 3:
                mBinding.actionBar.setTitle("版权转让");
                if (serializable != null){
                    fragmentTransaction.replace(R.id.rl_makeover_publish_up_content, CopyrightPublishFragment.newInstance((PublishCopyrightDetailEntity) serializable));
                }else {
                    fragmentTransaction.replace(R.id.rl_makeover_publish_up_content, CopyrightPublishFragment.newInstance());
                }
                break;
            case 4:
                mBinding.actionBar.setTitle("公司转让");
                if (serializable != null){
                    fragmentTransaction.replace(R.id.rl_makeover_publish_up_content, CompanyPublishFragment.newInstance((PublishCompanyDetailEntity) serializable));
                }else {
                    fragmentTransaction.replace(R.id.rl_makeover_publish_up_content, CompanyPublishFragment.newInstance());
                }
                break;
            case 5:
                mBinding.actionBar.setTitle("资产转让");
                if (serializable != null){
                    fragmentTransaction.replace(R.id.rl_makeover_publish_up_content, PropertyPublishFragment.newInstance((PublishPropertyDetailEntity) serializable));
                }else {
                    fragmentTransaction.replace(R.id.rl_makeover_publish_up_content, PropertyPublishFragment.newInstance());
                }
                break;
            default:
                break;
        }
        fragmentTransaction.commit();
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.getId() == R.id.tv_upload) {
            mLiveData.setValue(true);
        }
    }

    public File saveBitmapFile(Bitmap bitmap,String s){
        File file;
        String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/com.sgb.kjwl";
        if ("".equals(s)){
            //将要保存图片的路径
            file = new File(path+"01.jpg");
        }else {
            file = new File(path+s);
        }

        try {
            if(!new File(path).exists()){
                new File(path).mkdirs();
            }
            if (!file.exists()){
                file.createNewFile();
            }
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            bos.flush();
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == PictureConfig.CHOOSE_REQUEST) {
                mIntentLiveData.setValue(data);
            }
        }
    }
}
